package cn.itrip.auth.service.impl;

import cn.itrip.auth.service.ItripUserService;
import cn.itrip.auth.service.MailService;
import cn.itrip.auth.service.SmsService;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisAPI;
import cn.itrip.dao.user.ItripUserMapper;

import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
@Transactional
public class ItripUserServiceImpl implements ItripUserService {
    @Resource
    private ItripUserMapper itripUserMapper;
    @Resource
    private MailService mailService;
    @Resource
    private RedisAPI redisAPI;
    @Resource
    private SmsService smsService;
    @Override
    public void insertItripUser(ItripUser itripUser) throws Exception {
         itripUserMapper.insertItripUser(itripUser);
        String activationCode = MD5.getMd5(new Date().toLocaleString(), 32);
        mailService.sendActivationMail(itripUser.getUserCode(),activationCode);
        redisAPI.set("activation:"+itripUser.getUserCode(),30*60,activationCode);

    }


    @Override
    public boolean activate(String mail, String code) throws Exception {
    String value=redisAPI.get("activation:"+mail);
        if (value.equals(code)) {
            Map<String ,Object>param=new HashMap<>();
            param.put("userCode",mail);
            List<ItripUser> itripUserListByMap = itripUserMapper.getItripUserListByMap(param);
            if (itripUserListByMap.size()>0){
                ItripUser user=itripUserListByMap.get(0);
                user.setActivated(1);
                user.setFlatID(user.getId());
                user.setUserType(0);
                itripUserMapper.updateItripUser(user);
                return true;
            }
        }else {
            return  false;
        }

        return false;
    }

    @Override
    public ItripUser getItripUserCountByMap(String userCode) throws Exception {
        Map<String ,Object>param=new HashMap<>();
        param.put("userCode",userCode);
        List<ItripUser> users = itripUserMapper.getItripUserListByMap(param);
        if (users.size()>0){
            return users.get(0);
        }
        return null;
    }

    public void insertItripUserPhone(ItripUser user) throws Exception {
        itripUserMapper.insertItripUser(user);
        int code=MD5.getRandomCode();
        smsService.send(user.getUserCode(),"1",new String[]{String.valueOf(code),"1"});
        redisAPI.set("activation:"+user.getUserCode(),60,String.valueOf(code));
    }
    public  boolean validatePhone(String phoneNum,String code)throws Exception{
        String key="activation:"+phoneNum;
        String value=redisAPI.get(key);
        if (null!=value&&value.equals(code)){
            ItripUser user=this.getItripUserCountByMap(phoneNum);
            if (null!=user){
                user.setActivated(1);
                user.setFlatID(user.getId());
                user.setUserType(0);
                itripUserMapper.updateItripUser(user);
                return true;
            }
        }
        return false;
    }

    @SneakyThrows
    @Override
    public ItripUser login(String userCode, String userPassword) throws Exception {
        ItripUser user = this.getItripUserCountByMap(userCode);
        if (null!=user&&user.getUserPassword().equals(userPassword)){
            if (user.getActivated()!=1){
                throw new Exception("用户未激活");
            }
        }
        return user;
    }
}
